# coding = utf-8
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
#from PIL import Image
# from mahotas.features import surf
import tkinter as tk
from tkinter import filedialog

root = tk.Tk()
root.geometry("500x500")
root.title("灰度直方图测试")

# image = None
src_FILE = None


def get_padding():
    paddingTop = 650
    paddingButtom = 100
    padding_v = padding_e.get()
    padding_arr = padding_v.split(",")
    if padding_arr[0] == '':
        padding_arr = []
    if len(padding_arr) == 1:
        paddingTop = int(padding_arr[0])
    elif len(padding_arr) == 2:
        paddingTop = int(padding_arr[0])
        paddingButtom = int(padding_arr[1])
    return paddingTop, paddingButtom


def make_mask_padding(image):
    paddingTop, paddingButtom = get_padding()
    maskAll = np.zeros(image.shape[0:2], dtype="uint8")
    cv.rectangle(maskAll, (0, paddingTop), (image.shape[1], image.shape[0] - paddingButtom), 255, -1)
    gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)
    mask_out = cv.bitwise_and(gray, maskAll)
    cv.imwrite('imgs_out/hist_mask_out.jpg', mask_out)
    return maskAll


def choose_image():
    global src_FILE
    src_FILE = filedialog.askopenfilename(initialdir="images/", title="选择图片",
                                          filetypes=(("JPEG files", "*.jpg"), ("PNG files", "*.png")))
    show_e.set(src_FILE)


def do_image():
    # 导入资源
    image = cv.imread(src_FILE)  # r"D:\data\IMG_20230719_105059.jpg"
    #gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)
    padding = get_padding()
    image_roi = image[padding[0]:image.shape[0]-padding[1],]
    #mask_out = cv.bitwise_and(gray, maskAll)
    plt.hist(image_roi.ravel(), 256)
    plt.show()
    cv.waitKey(0)


def do_image3():
    # 导入资源
    image = cv.imread(src_FILE)  # r"D:\data\IMG_20230719_105059.jpg"
    # gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)
    maskAll = make_mask_padding(image)
    # mask_out = cv.bitwise_and(gray, maskAll)

    histb = cv.calcHist([image], [0], maskAll, [256], [0, 255])
    histg = cv.calcHist([image], [1], maskAll, [256], [0, 255])
    histr = cv.calcHist([image], [2], maskAll, [256], [0, 255])

    #cv.imshow("src", image)

    plt.plot(histb, color='b')
    plt.plot(histg, color='g')
    plt.plot(histr, color='r')

    plt.show()
    cv.waitKey(0)


button = tk.Button(root, text="选择图片", command=choose_image)
button.pack(side="top", pady=50)

show_e = tk.StringVar()
show_label = tk.Label(root, text='图片路径:', textvariable=show_e)
show_label.pack(side="top")

padding_label = tk.Label(root, text='padding')  # ,anchor='c').grid(row=0,column=0)
padding_label.pack(side="top")
padding_e = tk.StringVar()
padding = tk.Entry(root, width=20, textvariable=padding_e)  # .grid(row=0,column=1)
padding.pack(side="top")
# padding_e.set('输入padding参数，,分隔上下边距的像素')

button_do3 = tk.Button(root, text="三色直方图", command=do_image3)
button_do3.pack(side="bottom", pady=50)

button_do = tk.Button(root, text="灰度直方图", command=do_image)
button_do.pack(side="bottom", pady=50)

root.mainloop()
